package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.aspect.DataSource;
import com.yunc.common.utils.DataSourceType;
import com.yunc.common.utils.R;
import com.yunc.modules.shop.entity.Banner;
import com.yunc.modules.shop.service.IBannerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * banner轮播图
 *
 * @author congcong tao
 * @date 2018/12/24 9:27
 */
@RestController
@RequestMapping("banner")
public class BannerController extends CommonController {

    private static final Logger logger = LogManager.getLogger(BannerController.class);

    @Autowired
    private IBannerService bannerService;
    
    @GetMapping("/page")
    @DataSource(value = DataSourceType.SLAVE)
    public R page(Page<Banner> page, Banner banner){
        bannerService.page(page, new QueryWrapper<>(banner));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    @DataSource(value = DataSourceType.SLAVE)
    public R list(Banner banner){
        QueryWrapper<Banner> ew = new QueryWrapper<>();
        List<Banner> list = bannerService.list(ew);
        return R.ok().put("list", list).put("banner", banner);
    }

    @GetMapping("/info/{id}")
    @DataSource(value = DataSourceType.SLAVE)
    public R info(@PathVariable String id){
        Banner banner = bannerService.getById(id);
        return R.ok().put("banner", banner);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @DataSource(value = DataSourceType.SLAVE)
    public R save(@RequestBody Banner banner){
        banner.setCreateTime(new Date());
        boolean flag = bannerService.save(banner);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    @DataSource(value = DataSourceType.SLAVE)
    public R update(@RequestBody Banner banner){
        boolean flag = bannerService.updateById(banner);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
